<?php

/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 */

namespace Core\HTTP\Superglobal;

/**
 * Represent $_COOKIE superglobal in Controller.
 * @package Core
 * @subpackage HTTP.Superglobals
 * @author kovacsricsi, pilou
 * @version 0.2
 */
class Cookie extends ASuperglobal {

	/**
	 * Retrieve a value and return null if there is no element set.
	 * @access public
	 * @param  string $name
	 * @return mixed
	 */
	public function __get($key) {
		if (array_key_exists($key, $_COOKIE)) {
			return $_COOKIE[$key];
		} else {
			return null;
		}
	}

	/**
	 * Set a value
	 * @access public
	 * @param string $name
	 * @param array $value
	 * @return void
	 */
	public function __set($key, $value) {
		if (!is_array($value)) {
			$array = array("value" => $value);
		}
		$value		 = isset($array["value"])		? $array["value"]		: null;
		$expire		 = isset($array["expire"])		? $array["expire"]		: null;
		$path		 = isset($array["path"])		? $array["path"]		: '/';
		$domain		 = isset($array["domain"])		? $array["domain"]		: null;
		$secure		 = isset($array["secure"])		? $array["secure"]		: null;
		$httpOnly	 = isset($array["httpOnly"])	? $array["httpOnly"]	: null;
		
		return \Core\HTTP\HTTP::setCookie($key, $value, $expire, $path, $domain, $secure, $httpOnly);
	}

	/**
	 * Get the COOKIE data array
	 * @access public
	 * @return array
	 */
	public function toArray() {
		return $_COOKIE;
	}

}

?>